<template>
  <div class="list-container">
    <div class="list-top-bar">
      <el-link @click="goBack" :underline="false" class="back-link">
        <el-icon><ArrowLeft /></el-icon>
        返回
      </el-link>
      <el-button
        type="danger"
        size="large"
        @click="batchDelete"
        :disabled="selectedIds.length === 0"
        class="top-btn big-btn"
        >批量删除</el-button
      >
      <el-button
        size="large"
        @click="batchRead"
        :disabled="selectedIds.length === 0"
        class="top-btn big-btn"
        >批量已读</el-button
      >
    </div>
    <div class="list-header">
      <input type="checkbox" :checked="isAllSelected" @change="toggleAll" class="big-checkbox" />
      <span class="col subject">主题</span>
      <span class="col sender">发件人</span>
      <span class="col date">时间</span>
      <span class="col status">状态</span>
    </div>
    <Inboxitem
      v-for="item in mails"
      :key="item.id"
      :mail="item"
      :selected="selectedIds.includes(item.id)"
      @toggle="toggle"
      @goDetail="goDetail"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { getMailListAPI, deleteMailAPI, markMailReadAPI, type MailListItem } from '@/api/mailsAPI'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import Inboxitem from './InboxItem.vue'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const mails = ref<MailListItem[]>([])
const selectedIds = ref<number[]>([])

const fetchMails = async () => {
  const res = await getMailListAPI('inbox')
  if (res.code === 200) mails.value = res.data
}

const toggle = (id: number) => {
  if (selectedIds.value.includes(id)) {
    selectedIds.value = selectedIds.value.filter((i) => i !== id)
  } else {
    selectedIds.value.push(id)
  }
}

const isAllSelected = computed(
  () => mails.value.length > 0 && selectedIds.value.length === mails.value.length,
)
const toggleAll = () => {
  if (isAllSelected.value) {
    selectedIds.value = []
  } else {
    selectedIds.value = mails.value.map((item) => item.id)
  }
}

const batchDelete = async () => {
  const res = await deleteMailAPI(selectedIds.value)
  if (res.code === 200) {
    mails.value = mails.value.filter((item) => !selectedIds.value.includes(item.id))
    selectedIds.value = []
    ElMessage.success('删除成功')
  } else {
    ElMessage.error(res.msg || '删除失败')
  }
}

const batchRead = async () => {
  await Promise.all(selectedIds.value.map((id) => markMailReadAPI(id)))
  mails.value = mails.value.map((item) =>
    selectedIds.value.includes(item.id) ? { ...item, is_read: true } : item,
  )
  selectedIds.value = []
  ElMessage.success('标记已读')
}

const goDetail = async (id: number) => {
  try {
    await markMailReadAPI(id)
    mails.value = mails.value.map((item) => (item.id === id ? { ...item, is_read: true } : item))
  } catch (e) {
    ElMessage.warning('标记已读失败' + e)
  }
  router.push(`/mail/${id}`)
}

const goBack = () => {
  router.push('/home')
}

onMounted(fetchMails)
</script>

<style scoped>
.list-container {
  width: 100%;
  height: 100%;
  min-height: 100%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(209, 96, 83, 0.04);
  padding: 0;
}

.list-top-bar {
  display: flex;
  align-items: center;
  padding: 18px 24px 0 24px;
  gap: 16px;
  margin-bottom: 12px;
}
.back-link {
  color: #aaa;
  font-size: 16px;
  display: flex;
  align-items: center;
  transition: color 0.2s;
  font-weight: 500;
  margin-right: 12px;
}
.back-link .el-icon {
  margin-right: 4px;
  font-size: 18px;
}
.back-link:hover {
  color: #d16053;
}
.top-btn {
  min-width: 110px;
}
.big-btn {
  font-size: 18px !important;
  letter-spacing: 2px;
}

.list-header {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  border-bottom: 1px solid #eee;
  width: 100%;
  box-sizing: border-box;
  font-weight: bold;
  background: #f5f5f5;
  font-size: 16px;
}
.list-header .big-checkbox {
  width: 22px;
  height: 22px;
  accent-color: #409eff;
  margin-right: 18px;
  cursor: pointer;
}
.col.subject {
  display: inline-block;
  width: 320px;
  padding-right: 12px;
}
.col.sender {
  display: inline-block;
  width: 300px;
  padding-right: 12px;
}
.col.date {
  display: inline-block;
  width: 300px;
  padding-right: 12px;
}
.col.status {
  display: inline-block;
  width: 300px;
}
</style>
